<?php
/**
 * RPAC
 * @文件名称: RbacAssignModel.php
 * @author: jawei
 * @Email: gaozhiwei@etcp.cn
 * @Date: 2017-06-06
 * @Copyright: 2017 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
namespace app\models;
use app\components\DmpUtil;
use app\base\BaseException;
use app\base\BaseModel;

class RbacAssignModel extends BaseModel
{
    public static function tableName()
    {
        return 'dmp_rbac_assign';
    }

    public static function getByUserId($userId)
    {
        $rows = self::find()
            ->where('user_id = :ui', [':ui' => $userId])
            ->asArray()
            ->all();
        return $rows;
    }

    public static function getBatchByUserIds(array $userIds)
    {
        if (!DmpUtil::issetEmptyCheckBool($userIds)) {
            throw new BaseException('empty user id array in rbac assign model', 40332);
        }

        $idString = implode(',', $userIds);
        $where = "user_id in ({$idString})";
        $rows = self::find()
            ->where($where)
            ->asArray()
            ->all();
        return $rows;
    }

    public static function addOne($data)
    {
        $model = new RbacAssignModel();
        $model->user_id = $data['user_id'];
        $model->role_id = $data['role_id'];
        $model->role_name = $data['role_name'];
        $model->perm_ids = $data['perm_ids'];
        $model->routes = $data['routes'];
        $model->time_create = time();
        return $model->save();
    }

    public static function deleteByUserId($userId)
    {
        return RbacAssignModel::deleteAll('user_id = :ui', [':ui' => $userId]);
    }

    public static function getBatchByRoleIds(array $roleIds)
    {
        if (!DmpUtil::issetEmptyCheckBool($roleIds)) {
            throw new BaseException('empty user id array in rbac assign model', 40365);
        }

        $idString = implode(',', $roleIds);
        $where = "role_id in ({$idString})";
        $rows = self::find()
            ->where($where)
            ->asArray()
            ->all();
        return $rows;
    }
}
